import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq, fftshift

F = 5.
T = 20.
N = 10001
DT = T/N
t = np.linspace(0.,T,N)
t[0:5]

# Now we want to create some kind of signal. 
y = 2.*np.sin(2*np.pi*F*t)+1.*np.sin(2*np.pi*2*F*t)+0.5*np.sin(2*np.pi*4*F*t)

# Let's make a plot of this signal
plt.subplot(2, 1, 1)
plt.plot(t,y)
plt.grid(True)
plt.xlabel("time(s)",position=(0.95,1))
plt.ylabel("signal(V)",position=(1,0.9))
ax = plt.gca()
ax.set_xlim(0.,1.)

# We now want to see the Fourier Transform of this signal. 
y_fft = fftshift(fft(y))
x_fft = fftshift(fftfreq(N,DT))

plt.subplot(2, 1, 2)
plt.plot(x_fft,2./N*np.abs(y_fft)) # Note: the 2./N properly normalizes the FFT amplitude to be in Volts.
plt.grid(True)
ax = plt.gca()
ax.set_xlim(0,30)
# plt.yscale("log")      # Uncomment these to get a log-y scale.
# ax.set_ylim(1e-4,1)
plt.xlabel("Frequency[Hz]",position=(0.95,1))
plt.ylabel("signal[Volt]",position=(1,0.8))

plt.subplots_adjust(left=0.1, right=0.9, 
                    top=0.9, bottom=0.1, 
                    wspace=0.4, hspace=0.4)

plt.show()

